package com.example.dbg.controller;

import com.example.dbg.common.eumu.common.CommonYesOrNoEnum;
import com.example.dbg.common.eumu.common.ResultEnum;
import com.example.dbg.common.utils.DBGDateUtils;
import com.example.dbg.common.utils.FilterParamUtil;
import com.example.dbg.model.po.CuPassword;
import com.example.dbg.model.po.CuUser;
import com.example.dbg.model.vo.ResultDTO;
import com.example.dbg.service.ICuPasswordService;
import com.example.dbg.service.ICuUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.dbg.common.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

import static sun.misc.Version.println;

/**
 * @user DBG_ZXX
 * @date 2018/10/18 14:30
 * @desc
 **/
@Controller
@RequestMapping(value = "pwd")
public class PwdController {

    Logger logger = LoggerFactory.getLogger(PwdController.class);

    @Autowired
    ICuPasswordService cuPasswordService;

    @Autowired
    ICuUserService cuUserService;


    @RequestMapping(value = "pwdList/{id}" ,method = RequestMethod.GET)
    public String pwdList(HttpServletRequest request , Model model,@PathVariable String id){
        Map map = FilterParamUtil.getParameterMap(request,true);
        List<CuPassword> pwds = cuPasswordService.slectPwdList(Integer.parseInt(id));
        CuUser cuUser = cuUserService.selectById(Integer.parseInt(id));
        pwds.forEach(System.out::println);
        model.addAttribute("cuUserName" , cuUser.getLoginName());
        model.addAttribute("pwds",pwds);
        model.addAttribute("listSize",pwds.size());
        System.out.println("pwdList");
        return "/pwd/pwd-list";
    }


    @RequestMapping(value = "save" ,method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO save(HttpServletRequest request,CuPassword cuPassword){
        Map map = FilterParamUtil.getParameterMap(request,true);
        map.forEach((k,v) -> System.out.println(k+"="+v));
        Integer cuUserId = (Integer) request.getSession().getAttribute("cuUserId");
        cuPassword.setCuUserId(cuUserId);
        cuPassword.setIsDeleted(CommonYesOrNoEnum.NO.getCode());
        cuPassword.setGmtCreate(new Date());
        cuPassword.setGmtModified(new Date());
        System.out.println("save");
        System.out.println(cuPassword.toString());
        return cuPasswordService.insertPwd(cuPassword)?ResultDTO.build( ResultEnum.SUCC):ResultDTO.build(ResultEnum.FAIL);
    }

    /**
     * @desc: 删除
     * @param request
     * @retrun:  java.lang.String
     * @Date:    2018/10/25 19:17
     */
    @RequestMapping(value = "delete" ,method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO delete(HttpServletRequest request){

        Map map = FilterParamUtil.getParameterMap(request,true);
        map.forEach((k,v) -> System.out.println(k+"="+v));
        System.out.println("delete");
        return cuPasswordService.deletePwd(Integer.parseInt((String)map.get("id"))) ? ResultDTO.build( ResultEnum.SUCC)
                : ResultDTO.build(ResultEnum.FAIL);

    }

    /**
     * @desc: 批量删除
     * @param request
     * @retrun:  com.example.dbg.model.vo.ResultDTO
     * @Date:    2018/10/26 12:28
     */
    @RequestMapping(value = "deletePwds" ,method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO deletePwds(HttpServletRequest request){
        Map map = FilterParamUtil.getParameterMap(request,true);
        map.forEach((k,v) -> System.out.println(k+"="+v));
        String strs = map.get("ids").toString();
        String[] idsStr = strs.split(",");
        List<Integer> list = new ArrayList();
        Arrays.asList(idsStr).forEach(idStr -> {
            Integer id = Integer.parseInt(idStr.trim());
            list.add(id);
        });
        System.out.println(list);
        System.out.println("delete");
        return cuPasswordService.deletePwds(list) ? ResultDTO.build( ResultEnum.SUCC) : ResultDTO.build(ResultEnum.FAIL);

    }

    /**
     * @desc: 得到更新页面
     * @param request
     * @param model
     * @retrun:  java.lang.String
     * @Date:    2018/10/26 12:28
     */
    @RequestMapping(value = "updateHtml" ,method = RequestMethod.GET)
    public String updateHtml(HttpServletRequest request ,Model model){

        Map map = FilterParamUtil.getParameterMap(request,true);
        CuPassword cPwd = cuPasswordService.selectOneById(Integer.parseInt((String)map.get("id")));
        model.addAttribute("cPwd",cPwd);
        System.out.println("updateHtml");

        return "/pwd/pwd-update";
    }

    /**
     * @desc: 更新数据
     * @param request
     * @param cuPassword
     * @retrun:  com.example.dbg.model.vo.ResultDTO
     * @Date:    2018/10/26 12:29
     */
    @ResponseBody
    @RequestMapping(value = "update" ,method = RequestMethod.POST)
    public ResultDTO update(HttpServletRequest request,CuPassword cuPassword){
        Map map = FilterParamUtil.getParameterMap(request,true);
        map.forEach((k,v) -> System.out.println(k+"="+v));
        System.out.println(cuPassword.toString());
        cuPassword.setIsDeleted(CommonYesOrNoEnum.NO.getCode());
        cuPassword.setGmtModified(new Date());
        boolean status = cuPasswordService.updatePwd(cuPassword);
        System.out.println("update");
        return status?ResultDTO.build(ResultEnum.SUCC):ResultDTO.build(ResultEnum.FAIL);
    }

    @RequestMapping(value = "getListByTimeAndName" ,method = RequestMethod.POST)
    public String getListByTimeAndName(HttpServletRequest request, Model model , HttpSession session){

        Map<String,Object> maps = FilterParamUtil.getParameterMap(request,true);

        maps.forEach((k,v) -> System.out.println(k + "=" + v));

        int cuUserId = (int)request.getSession().getAttribute("cuUserId");
        String name = StringUtils.isNullOrEmpty(maps.get("name")) ? null : maps.get("name").toString();
        String begin = StringUtils.isNullOrEmpty(maps.get("begin")) ? null : maps.get("begin").toString();
        String end = StringUtils.isNullOrEmpty(maps.get("end")) ? null : maps.get("end").toString();
        System.out.println(name);
        List<CuPassword> pwds = cuPasswordService.getListByTimeAndName(begin , end , cuUserId,name );
        pwds.forEach(pwd -> System.out.println(pwd.toString()));
        model.addAttribute("pwds",pwds);
        model.addAttribute("listSize",pwds.size());
        System.out.println("getListByTimeAndName");

        return "/pwd/pwd-list::table_refresh";
    }


    @RequestMapping(value = "pwdsSize" , method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO pwdsSize(HttpServletRequest request , HttpSession session){

        Map<String,Object> maps = FilterParamUtil.getParameterMap(request,true);

        maps.forEach((k,v) -> System.out.println(k + "=" + v));

        int cuUserId = (int)request.getSession().getAttribute("cuUserId");
        String name = StringUtils.isNullOrEmpty(maps.get("name")) ? null : maps.get("name").toString();
        String begin = StringUtils.isNullOrEmpty(maps.get("begin")) ? null : maps.get("begin").toString();
        String end = StringUtils.isNullOrEmpty(maps.get("end")) ? null : maps.get("end").toString();
        List<CuPassword> pwds = cuPasswordService.getListByTimeAndName(begin , end , cuUserId,name );
        logger.info("pwdsSize");
        return ResultDTO.build(ResultEnum.SUCC,pwds.size());
    }

    @RequestMapping(value = "selectOneById" ,method = RequestMethod.GET)
    public String selectOneById(HttpServletRequest request){
        Map map = FilterParamUtil.getParameterMap(request,true);
        System.out.println("selectOneById");
        return "/pwd/pwd-list";
    }


}
